import { CellRenderMap, INDEX, COMPANY_NAME, BUSINESS_DATA, REGISTERED_CAPITAL, PROVINCE, CITY, COUNTRY, INDUSTRY_CATEGORY, INFO_SOURCE } from '@/utils/tableColumn';
import { emptyHandle, isNull } from '@vc/utils';
import { ModuleEnum } from './constants';
import type { IColumnsOption } from 'vc';
import { setHrefUrlTool } from '@/utils';

export function useTable(moduleName: ModuleEnum, versionMaxIndex?) {
  /** 限期整改 */
  const rectify_columns: IColumnsOption[] = [
    { ...INDEX },
    {
      field: 'notificationNumber',
      title: '整改通知书编号',
      width: 260,
      sortable: true,
      sortBy: 'notificationNumber',
      showOverflow: 'ellipsis',
      cellRender: {
        name: CellRenderMap.HighLight
      }
    },
    {
      ...INFO_SOURCE(''),
      width: 260,
      title: '限期整改通知书'
    },
    {
      width: 260,
      title: '下达时间',
      field: 'issuedDate',
      align: 'center',
      sortable: true,
      sortBy: 'issuedDate'
    },
    {
      title: '详情',
      align: 'center',
      slots: {
        default({ row }) {
          return row.details?.length ? (
            <>
              <span class="common-link" onClick={() => openDetailModal(row.details)}>
                查看
              </span>
            </>
          ) : (
            '-'
          );
        }
      }
    }
    
  ];

  /** 登记信息 */
  const register_columns: IColumnsOption[] = [
    { ...INDEX },
    {
      field: 'registrationNumber',
      title: '登记编号',
      width: 230,
      sortable: true,
      sortBy: 'registrationNumber',
      slots: {
        default: ({ row }) => emptyHandle(row.changes?.[versionMaxIndex]?.registrationNumber)
      }
    },
    {
      ...INFO_SOURCE(''),
      title: '登记回执'
    },
    {
      field: 'topChangeTypeCode',
      title: '登记类型',
      width: 120,
      sortable: true,
      sortBy: 'topChangeTypeCode',
      align: 'center',
      slots: {
        default: ({ row }) => {
          if (row.changes?.length || row.changeType) {
            let c: any = '';
            if (row.changes?.length) {
              c = <a onClick={() => openDetailModal(row.changes)}>查看变更{row.changes.length}</a>;
            }
            return (
              <div>
                {row.changeType} {c}
              </div>
            );
          }
          return '-';
        }
      }
    },
    {
      title: '登记时间',
      field: 'registrationDate',
      width: 110,
      align: 'center',
      sortable: true,
      sortBy: 'registrationDate',
      slots: {
        default: ({ row }) => emptyHandle(row.changes?.[versionMaxIndex]?.registrationDate)
      }
    },
    {
      field: 'periodOfValidityStartDate',
      title: '有效期限',
      minWidth: 190,
      align: 'center',
      sortable: true,
      sortBy: 'periodOfValidityStartDate',
      slots: {
        default: ({ row }) => emptyHandle(row.changes?.[versionMaxIndex]?.periodOfValidityStartDate) + ' 至 ' + emptyHandle(row.changes?.[versionMaxIndex]?.periodOfValidityExpirationDate)
      }
    },
    {
      width: 270,
      field: 'productionSite',
      title: '生产经营场所',
      showOverflow: 'title'
    }
  ];

  /** 执行报告 */
  const executeTheReport_columns: IColumnsOption[] = [
    { ...INDEX },
    {
      field: 'dischargePermitNumber',
      title: '排污许可证编号',
      width: 230,
      sortable: true,
      showOverflow: 'ellipsis',
      align: 'center',
      cellRender: {
        name: CellRenderMap.HighLight
      }
    },
    {
      field: 'latelyReport',
      title: '执行报告',
      width: 160,
      align: 'center',
      slots: {
        default: ({ row }) => {
          if (row?.latelyReport?.fileUrl) {
            return <a onClick={() => setHrefUrlTool(row.latelyReport?.fileType, row.latelyReport?.fileUrl)} v-html={row.latelyReport?.reportTitle}></a>;
          } else {
            return <span v-html={row.latelyReport?.reportTitle || '-'}></span>;
          }
        }
      }
    },
    {
      title: '更多执行报告',
      field: 'reports',
      width: 110,
      align: 'center',
      slots: {
        default({ row }) {
          return row?.reports?.length ? (
            <>
              <a class="details" onClick={() => openDetailModal(row.reports)}>
                更多 {row?.reports?.length}
              </a>
            </>
          ) : (
            '-'
          );
        }
      }
    },
    {
      width: 100,
      title: '报告类型',
      field: 'reportType',
      align: 'center',
      sortable: true,
      formatter({ row }) {
        return row?.latelyReport?.reportType ?? '-';
      }
    },
    {
      width: 110,
      title: '报告日期',
      field: 'noticeDate',
      align: 'center',
      sortable: true,
      slots: {
        default({ row }) {
          return <span v-html={row?.latelyReport?.noticeDate ?? '-'}></span>;
        }
      }
    },
    {
      title: '生产经营场所',
      field: 'productionSite',
      showOverflow: 'title',
      headerAlign: 'center'
    }
  ];

  /** 撤消/注销 */
  const log_off_columns: IColumnsOption[] = [
    { ...INDEX },
    {
      field: 'dischargePermitNumber',
      title: '排污许可证编号',
      width: 230,
      sortable: true,
      showOverflow: 'ellipsis',
      cellRender: {
        name: CellRenderMap.HighLight
      },
      align: 'center'
    },
    {
      width: 150,
      field: 'industryClassification',
      title: '行业类别',
      align: 'center'
    },
    {
      width: 150,
      field: 'type',
      title: '类型',
      align: 'center',
      slots: {
        default({ row }) {
          return <span>{row.type === '1' ? '许可证撤销' : row.type === '2' ? '许可证注销' : row.type === '3' ? '许可证遗失' : ''}</span>;
        }
      }
    },
    {
      width: 150,
      field: 'date',
      title: '时间',
      align: 'center'
    },
    {
      field: 'reason',
      title: '原因',
      headerAlign: 'center'
    }
  ];
  
  const showDetailModal = ref(false);
  const detailsTableData = ref([]);
  const openDetailModal = (data) => {
    detailsTableData.value = data;
    showDetailModal.value = true;
  };
  
  /** 执行报告详情 */
  const executeTheReport_detailsColumn = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      title: '报告类型',
      field: 'reportType',
      width: 100,
      align: 'center'
    },
    {
      title: '报告日期',
      field: 'noticeDate',
      width: 100,
      align: 'center'
    },
    {
      title: '执行报告',
      field: 'reportTitle',
      minWidth: 120,
      align: 'center',
      slots: {
        default: ({ row }) => {
          if (row?.fileUrl) {
            return <a onClick={() => setHrefUrlTool(row.fileType, row.fileUrl)} v-html={row?.reportTitle}></a>;
          } else {
            return <span v-html={row?.reportTitle || '-'}></span>;
          }
        }
      }
    }
  ]);
  
  /** 限期整改详情 */
  const rectify_detailsColumn = ref<IColumnsOption[]>([
    { ...INDEX },
    {
      width: 180,
      title: '整改时限',
      field: 'issuedDate',
      align: 'center',
      formatter({ row }) {
        return `${row.timeLimitStartDate || '-'} 至 ${row.timeLimitExpirationDate || '-'}`;
      }
    },
    {
      minWidth: 110,
      title: '整改问题',
      field: 'problem',
      showOverflow: 'title'
    },
    {
      minWidth: 110,
      title: '整改措施',
      field: 'correctiveActions',
      showOverflow: 'title'
    },
    {
      minWidth: 110,
      title: '整改计划',
      field: 'correctiveActionPlan',
      showOverflow: 'title'
    },
    {
      width: 110,
      title: '是否完成整改',
      field: 'tagCompleted',
      align: 'center',
      formatter({ cellValue }) {
        return isNull(cellValue) ? '-' : cellValue === 1 ? '是' : '否';
      }
    }
  ]);
  
  /** 登记信息详情 */
  const detailChangesColumn = ref<IColumnsOption[]>([
    { field: 'registrationNumber', align: 'center', title: '登记编号',minWidth:200  },
    { field: 'changeType', align: 'center', title: '登记类型', width: 100 },
    { field: 'version', align: 'center', title: '版本', width: 100 },
    { field: 'registrationDate', align: 'center', title: '登记日期', width: 100 },
    {
      align: 'center',
      title: '有效期限',
      width: 200,
      formatter: ({ row }) => {
        return `${row?.periodOfValidityStartDate ?? '-'} 至 ${row?.periodOfValidityExpirationDate ?? '-'}`;
      }
    }
  ]);
  
  return {
    columns:
      moduleName === ModuleEnum.RECTIFICATION_WITHIN_A_TIME_LIMIT
        ? rectify_columns
        : moduleName === ModuleEnum.REGISTRATION_INFORMATION
        ? register_columns
        : moduleName === ModuleEnum.EXECUTE_THE_REPORT
        ? executeTheReport_columns
        : log_off_columns,
    showDetailModal,
    detailsTableData,
    detailsColumn:  moduleName === ModuleEnum.RECTIFICATION_WITHIN_A_TIME_LIMIT
      ? rectify_detailsColumn
        : moduleName === ModuleEnum.EXECUTE_THE_REPORT
          ? executeTheReport_detailsColumn
          : detailChangesColumn
  };
}
